package util;

import java.util.Objects;

/**
 * Provides some convenience methods for validation.
 * 
 * @author Henrik Wall
 * @author Rudolf Schmidt
 * @author Eugen Nemirowski
 */
public final class Validator {
	/**
	 * Don't let anyone instantiate this class.
	 */
	private Validator() {}
	
	/**
	 * Checks whether an object reference is {@code null}.
	 * @param obj the reference to be checked
	 * @param message for the possible exception
	 * @return the reference
	 * @throws NullPointerException if the reference is {@code null}
	 * @see Objects#requireNonNull(Object, String)
	 */
	public static <T> T nonNull(final T obj, final String message) {
		return Objects.requireNonNull(obj, message);
	}
	
	/**
	 * Checks whether a character sequence is empty.
	 * @param charSequence the character sequence to be checked
	 * @param message for the possible exception
	 * @return the character sequence
	 * @throws NullPointerException if the character sequence is {@code null}
	 * @throws IllegalArgumentException if the character sequence is empty
	 * @see #nonNull(Object, String)
	 */
	public static <T extends CharSequence> T nonEmpty(final T charSequence, final String message) {
		if (nonNull(charSequence, message).length() == 0) {
			throw new IllegalArgumentException(message);
		}
		return charSequence;
	}
	
	/**
	 * Checks whether a number is zero.
	 * @param number the number to be checked
	 * @param message for the possible exception
	 * @return the number
	 * @throws IllegalArgumentException if the number is zero
	 */
	public static <T extends Number> T nonZero(final T number, final String message) {
		if (number.doubleValue() == 0.)
			throw new IllegalArgumentException(message);
		return number;
	}
	
	/**
	 * Checks whether a number is negative.
	 * @param number the number to be checked
	 * @param message for the possible exception
	 * @return the number
	 * @throws IllegalArgumentException if the number is negative
	 */
	public static <T extends Number> T nonNegative(final T number, final String message) {
		if (number.doubleValue() < 0.)
			throw new IllegalArgumentException(message);
		return number;
	}
}